package com.mall.web.controller.product;

import com.mall.common.annotation.Log;
import com.mall.common.config.Global;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.file.FileUploadUtils;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.product.domain.entity.ProBrand;
import com.mall.product.service.ProBrandService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 品牌Controller
 *
 * @author zhonglin
 * @date 2020-04-26
 */
@Controller
@RequestMapping("/product/brand")
public class ProBrandController
    extends BaseController {

  private static final Logger log = LoggerFactory.getLogger(ProBrandController.class);

  private String prefix = "product/brand";

  @Autowired
  private ProBrandService proBrandService;

  @RequiresPermissions("product:brand:view")
  @GetMapping()
  public String brand() {

    return prefix + "/brand";
  }

  /**
   * 查询品牌列表
   */
  @RequiresPermissions("product:brand:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProBrand proBrand) {

    startPage();
    List<ProBrand> list = proBrandService.selectProBrandList(proBrand);
    return getDataTable(list);
  }

  /**
   * 导出品牌列表
   */
  @RequiresPermissions("product:brand:export")
  @Log(title = "品牌", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProBrand proBrand) {

    List<ProBrand>      list = proBrandService.selectProBrandList(proBrand);
    ExcelUtil<ProBrand> util = new ExcelUtil<ProBrand>(ProBrand.class);
    return util.exportExcel(list, "brand");
  }

  /**
   * 新增品牌
   */
  @GetMapping("/add")
  public String add() {

    return prefix + "/add";
  }

  /**
   * 新增保存品牌
   */
  @RequiresPermissions("product:brand:add")
  @Log(title = "品牌", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProBrand proBrand,
                            @RequestParam(value = "file", required = false) MultipartFile file) {

    try {
      if (file != null && !file.isEmpty()) {
        String url = FileUploadUtils.upload(Global.getAvatarPath(), file);
        proBrand.setLogoUrl(url);
      }
    } catch (Exception e) {
      log.error("新增品牌失败！", e);
      return error(e.getMessage());
    }
    return toAjax(proBrandService.insertProBrand(proBrand));
  }

  /**
   * 修改品牌
   */
  @GetMapping("/edit/{brandId}")
  public String edit(@PathVariable("brandId") Long brandId,
                     ModelMap mmap) {

    ProBrand proBrand = proBrandService.selectProBrandById(brandId);
    mmap.put("proBrand", proBrand);
    return prefix + "/edit";
  }

  /**
   * 修改保存品牌
   */
  @RequiresPermissions("product:brand:edit")
  @Log(title = "品牌", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProBrand proBrand,
                             @RequestParam(value = "file", required = false) MultipartFile file) {
    try {
      if (file != null && !file.isEmpty()) {
        String url = FileUploadUtils.upload(Global.getAvatarPath(), file);
        proBrand.setLogoUrl(url);
      }
    } catch (Exception e) {
      log.error("新增品牌失败！", e);
      return error(e.getMessage());
    }
    return toAjax(proBrandService.updateProBrand(proBrand));
  }

  /**
   * 删除品牌
   */
  @RequiresPermissions("product:brand:remove")
  @Log(title = "品牌", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proBrandService.deleteProBrandByIds(ids));
  }

}
